﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TCC.CMFlow.Models;
using TCC.CMFlow.Contexts;
using TCC.CMFlow.Models.Enumerators;

namespace TCC.CMFlow.Controllers
{
    [Authorize]
    public class LogSistemaController : Controller
    {
        private DataContext db = new DataContext();

        // GET: /LogSistema/
        [AuthorizeUserAttribute(Modulo = eModulosSistema.LogSistema)]
        public ActionResult Index()
        {
            var modulos = db.ModuloSistemas.ToList();
            ViewBag.ModuloLog = new SelectList(modulos, "ID", "Nome");

            var logsistema = db.LogSistema.Include(l => l.Usuario);
            return View(logsistema.ToList());
        }

        [AuthorizeUserAttribute(Modulo = eModulosSistema.LogSistema, Permissao = ePermissoes.Consultar)]
        public ActionResult Search(string ID, string ModuloLog, string acaoDe, string acaoAte)
        {
            long _ID;
            long.TryParse(ID, out _ID);

            long _IDModulo;
            long.TryParse(ModuloLog, out _IDModulo);

            DateTime _acaoDe;
            DateTime.TryParse(acaoDe, out _acaoDe);

            DateTime _acaoAte;
            DateTime.TryParse(acaoAte, out _acaoAte);

            var modulos = db.ModuloSistemas.ToList();
            ViewBag.ModuloLog = new SelectList(modulos, "ID", "Nome", ModuloLog);

            var logs = from l in db.LogSistema
                            select l;

            if (_ID > 0)
            {
                logs = logs.Where(g => g.ID == _ID);
            }

            if (_IDModulo > 0)
            {
                logs = logs.Where(w => w.Modulo.ID == _IDModulo);
            }

            if (_acaoDe != null && _acaoDe != DateTime.MinValue)
            {
                logs = logs.Where(w => w.DataHoraAcao >= _acaoDe);
            }

            if (_acaoAte != null && _acaoAte != DateTime.MinValue)
            {
                _acaoAte = _acaoAte.AddHours(23).AddMinutes(59).AddSeconds(59);

                logs = logs.Where(w => w.DataHoraAcao <= _acaoAte);
            }

            return View("Index", logs.ToList());
        }

        // GET: /LogSistema/Details/5
        [AuthorizeUserAttribute(Modulo = eModulosSistema.LogSistema, Permissao = ePermissoes.Visualizar)]
        public ActionResult Details(long? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LogSistema logsistema = db.LogSistema.Find(id);
            if (logsistema == null)
            {
                return HttpNotFound();
            }
            return View(logsistema);
        }

        public ActionResult TipoLog(EnumTipoLog tipoLog)
        {
            switch (tipoLog)
            {
                case EnumTipoLog.Incluido:
                    return Content("<span class=\"text-success glyphicon glyphicon-plus-sign\"></span> " + EnumTipoLog.Incluido.ToString());
                case EnumTipoLog.Alterado:
                    return Content("<span class=\"text-info glyphicon glyphicon-minus-sign\"></span> " + EnumTipoLog.Alterado.ToString());
                case EnumTipoLog.Excluido:
                    return Content("<span class=\"text-warning glyphicon glyphicon-remove-sign\"></span> " + EnumTipoLog.Excluido.ToString());
                case EnumTipoLog.Inativado:
                    return Content("<span class=\"text-danger glyphicon glyphicon-ban-circle\"></span> " + EnumTipoLog.Excluido.ToString());
                case EnumTipoLog.Erro:
                    return Content("<span class=\"text-danger glyphicon glyphicon-exclamation-sign\"></span> " + EnumTipoLog.Erro.ToString());
                case EnumTipoLog.Rotina:
                    return Content("<span class=\"text-default glyphicon glyphicon-cog\"></span> " + EnumTipoLog.Rotina.ToString());
                default:
                    return Content("-");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
